<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/9/29
 * Time: 15:37
 */
namespace app\admin\controller;
use think\Db;
use app\admin\model\MaterModel;
class Mater extends Base
{
    /*
     * 物料订单数据
     */
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            $map = [];
            if(isset($key)&&$key!="")
            {
                $map['so_number'] = ['like',"%" . $key . "%"];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end=="")
            {
                $map['so_time'] = ['>= time',$start];
            }
            if(isset($end)&&$end!=""&&isset($start)&&$start=="")
            {
                $map['so_time'] = ['<= time',$end];
            }
            if(isset($start)&&$start!=""&&isset($end)&&$end!="")
            {
                $map['so_time'] = ['between time',[$start,$end]];
            }
            $map['so_status'] = ['in','2,3'];
            $Nowpage = input('page') ? input('page'):1;
            $limits = input("limit")?input("limit"):10;// 获取总条数;
            $user = new MaterModel();
            $count = $user->getMaterCount($map);
            $lists = $user->getMaterData($map,$Nowpage, $limits);
            return json(['code'=>0,'msg'=>'','count'=>$count,'data'=>$lists]);
        }
        return $this->fetch('mater/index');
    }

    /*
     * 订单详情
     */
    public function orderDetail(){
        $user = new MaterModel();
        $data = $user->getOrderDetail(['so_id'=>input('id')]);
        return $this->fetch('mater/order_detail',['data'=>$data]);
    }

    /*
     * 修改配送信息
     */
    public function delivery(){
        $user = new MaterModel();
        $res = $user->save(['so_send_status'=>2],['so_id'=>input('id')]);
        if($res){
            return json(['code'=>200,'msg'=>'配送成功']);
        }else{
            return json(['code'=>100,'msg'=>'配送失败']);
        }
    }

    /*
     * 处理退款
     */
    public function agreetui(){
        extract(input());
        $user = new MaterModel();
        $tui = new \app\shop\controller\Base();
        if($num == 3){
            $result = $tui->refundAdmin($id);
            if($result['status'] == 200){
                return json(['code'=>200,'msg'=>'退款处理成功']);
            }else{
                return json(['code'=>100,'msg'=>$result['msg']]);
            }
        }else{
            $res = $user->save(['so_in_out'=>$num],['so_id'=>$id]);
            if($res){
                return json(['code'=>200,'msg'=>'已拒绝']);
            }else{
                return json(['code'=>100,'msg'=>'操作失败，请联系管理员']);
            }
        }


    }

    /*
     * 修改物流
     */
    public function wuliuEdit(){
        $user = new MaterModel();
        if(request ()->isPost ()){
            extract(input());
            $param  = [
                'so_wuliu_name'=>$name,
                'so_wuliu_num'=>$num,
                'so_update_time'=>date('Y-m-d H:i:s')
            ];
            $res = Db::name('shop_order')->where('so_id',$id)->update($param);
            if($res){
                return json(['code'=>200,'msg'=>'填写成功']);
            }else{
                return json(['code'=>100,'msg'=>'填写失败']);
            }
        }
        $data = $user->where('so_id',input('id'))->field('so_id,so_wuliu_name,so_wuliu_num')->find();
        return $this->fetch('mater/wuliu_edit',['data'=>$data]);
    }
}